home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_toolbx.arc / FDIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-30  |  2.8 KB  |  115 lines

  1. /* fdio.c file - I/O module for FD program  */
  2. #include "stdio.h"
  3. #include "cminor.h"
  4. #include "fdparm.h"
  5.  
  6. /* fseek mode definitions */
  7. #define CURRENT_REL    1    /* relative to current position */
  8. #define EOF_REL     2    /* relative to end of the file    */
  9. #define BOF_REL     0    /* relative to beginning of file*/
  10. #define CTL_Z           26    /* marks EOF in some text files */
  11.  
  12. FILE *fp   ;            /* file pointer for text file    */
  13. long filesize ;         /* size of the file in bytes    */
  14. long int   ftell() ;
  15. FILE *gfopen()     ;
  16.  
  17. int open_file(fn)        /* open a file    */
  18.   char    fn[]  ;         /* file name string    */
  19.   {                /* return sucess or failure    */
  20.      fp = gfopen(fn,"r",BIN_MODE)  ;
  21.      if(fp != NULL)
  22.     return(SUCCESS)  ;
  23.      else return(FAILURE)  ;
  24.   }
  25.  
  26. int set_filesize(chk_ctlz)    /* set up filesize    */
  27.   int    chk_ctlz  ;        /* if = ASC_MODE check for CONT-Z*/
  28.   {
  29.      long pos  ;
  30.      int c  ;
  31.  
  32.      fseek(fp,0L,EOF_REL)  ;    /* get size of file    */
  33.      filesize = ftell(fp)  ;
  34.  
  35.      if( chk_ctlz != ASC_MODE )        /* look for control Z   */
  36.     return    ;
  37.  
  38.      /* check last 128 byte block for CTL-Z  */
  39.      pos = filesize - (filesize % 128)    ;
  40.      fseek(fp,pos,BOF_REL)  ;
  41.      c = getc(fp)  ;
  42.      while( c != EOF )
  43.     {  if( c == CTL_Z )    /* look for control-Z    */
  44.          {    filesize = ftell(fp) - 1  ;
  45.         return    ;    /* found - adjust file size & exit */
  46.          }
  47.        c = getc(fp)  ;
  48.     }
  49.   }
  50.  
  51.  
  52. int move_to(new_pos)        /* move to a specified position */
  53.   long new_pos ;
  54.   {
  55.       fseek(fp, new_pos ,BOF_REL)  ;
  56.   }
  57.  
  58. long where_now()        /* return current position    */
  59.   {
  60.      return(ftell(fp) )  ;
  61.   }
  62.  
  63. int  get_next_char()        /* get char at file position    */
  64.   {
  65.      char  c  ;
  66.  
  67.      if( ftell(fp) == filesize )    /* are we at end of file ? */
  68.     return( EOF_MARK )  ;        /* yes - return eof mark  */
  69.      else                /* no - get next char    */
  70.     {  c = getc(fp)  ;        /* get a character    */
  71.        return( toascii(c) )  ;    /* force char to ASCII, return it */
  72.     }
  73.   }
  74.  
  75.  
  76. int  get_previous_char()        /* read the char in front of */
  77.   {                    /* current file position     */
  78.      char  c  ;
  79.  
  80.      if( ftell(fp) != 0L )        /* check for beginning of file */
  81.     {  fseek(fp, -1L , CURRENT_REL ) ;    /* back up one char  */
  82.        c = getc(fp)  ;            /* read a char    */
  83.                 /* back up in front of char read  */
  84.        fseek(fp, -1L, CURRENT_REL  ) ;
  85.        return( toascii(c) )  ;    /* force it to ASCII  */
  86.     }                /* and return it      */
  87.      else  return(EOF_MARK)  ;
  88.   }
  89.  
  90. int  close_file()
  91.   {
  92.      fclose(fp)  ;
  93.   }
  94.  
  95.  
  96. int  read_line(buf,nread)    /* read one line of characters    */
  97.   char    buf[]  ;        /* put the data here  */
  98.   int    nread  ;        /* maximum number of bytes to read  */
  99.   {
  100.      int   i , c  ;
  101.  
  102.      i = 0  ;
  103.      for( i=1 ; i < nread ; i= i+1 )
  104.     {
  105.        c = getc(fp)  ;
  106.        if( c == EOF )
  107.           break  ;
  108.        buf[i] = c  ;
  109.     }
  110.      return( i )  ;        /* return number of chars read    */
  111.   }
  112.  
  113.  
  114.  
  115.